package cn.com.xinjinfu.pdoulcProduct.widget;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.com.xinjinfu.pdoulcProduct.MainActivity;
import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.utils.SharedPreferencesUtils;

/**
 *
 * 手势密码设置界面
 *
 */
public class GestureEditActivity extends Activity implements OnClickListener {
	/** 意图 */
	public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE";
	/** 首次提示绘制手势密码，可以选择跳过 */
	public static final String PARAM_IS_FIRST_ADVICE = "PARAM_IS_FIRST_ADVICE";
	private TextView mTextTitle;
	private LockIndicator mLockIndicator;
	private TextView mTextTip;
	private FrameLayout mGestureContainer;
	private GestureContentView mGestureContentView;
	private TextView mTextReset;
	private String mParamSetUpcode = null;
	private String mParamPhoneNumber;
	private boolean mIsFirstInput = true;
	private String mFirstPassword = null;
	private String mConfirmPassword = null;
	private int mParamIntentCode;
	RelativeLayout top_layout;
	String flag;
	private final String ACTION_NAME = "跳转首页广播";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gesture_edit);
		ActivityManager.getInstance().addActivity(this);
		setUpViews();
		setUpListeners();
		/**
		 * 登陆完成后第一次进入时，要进行手势密码设置，可跳过
		 * flag 0为第一次设置，1为普通设置
		 */
		Intent intent = getIntent();

		flag = intent.getStringExtra("flag");
		if (flag.equals("1")){
			top_layout.setVisibility(View.VISIBLE);
			mTextReset.setVisibility(View.GONE);
		}else if(flag.equals("0")){
			top_layout.setVisibility(View.INVISIBLE);
			mTextReset.setVisibility(View.VISIBLE);
		}
	}

	private void setUpViews() {
		top_layout=(RelativeLayout)findViewById(R.id.top_layout);
		top_layout.setOnClickListener(this);
		mTextReset = (TextView) findViewById(R.id.text_reset);
		mTextReset.setClickable(true);
		mLockIndicator = (LockIndicator) findViewById(R.id.lock_indicator);
		mTextTip = (TextView) findViewById(R.id.text_tip);
		mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container);
		// 初始化一个显示各个点的viewGroup
		mGestureContentView = new GestureContentView(this, false, "", new GestureDrawline.GestureCallBack() {
			@Override
			public void onGestureCodeInput(String inputCode) {
				if (!isInputPassValidate(inputCode)) {
					mTextTip.setText(Html.fromHtml("<font color='#FF0000'>最少链接4个点, 请重新输入</font>"));
					mGestureContentView.clearDrawlineState(0L);
					return;
				}
				if (mIsFirstInput) {
					mFirstPassword = inputCode;
					updateCodeList(inputCode);
					mGestureContentView.clearDrawlineState(0L);
					if(flag.equals("1")){
						mTextReset.setClickable(true);
						mTextReset.setVisibility(View.VISIBLE);
						mTextReset.setText("重新设置手势密码");
					}

				} else {
					if (inputCode.equals(mFirstPassword)) {
						mTextTip.setText(Html.fromHtml("<font color='#F77F96'>密码设置成功</font>"));
						SharedPreferencesUtils.saveUserInfo(GestureEditActivity.this, "mShoushiPassword", mFirstPassword);
						SharedPreferencesUtils.savePwdBoolean(GestureEditActivity.this, "is_ok", true);

						if (flag.equals("1")){
							finish();
						}else if(flag.equals("0")){
							startActivity(new Intent(GestureEditActivity.this, MainActivity.class));
							ActivityManager.getInstance().exitActivity();
							Intent mIntent = new Intent(ACTION_NAME);
							mIntent.putExtra("yaner", "shouye");
							//发送广播
							sendBroadcast(mIntent);
						}
						mGestureContentView.clearDrawlineState1(1300L);

					} else {
						mTextTip.setText(Html.fromHtml("<font color='#FF0000'>与上一次绘制不一致，请重新绘制</font>"));
						// 左右移动动画
						Animation shakeAnimation = AnimationUtils.loadAnimation(GestureEditActivity.this, R.anim.shake);
						mTextTip.startAnimation(shakeAnimation);
						// 保持绘制的线，1.5秒后清除
						mGestureContentView.clearDrawlineState(1300L);
					}
				}
				mIsFirstInput = false;
			}

			@Override
			public void checkedSuccess() {

			}

			@Override
			public void checkedFail() {

			}
		});
		// 设置手势解锁显示到哪个布局里面
		mGestureContentView.setParentView(mGestureContainer);
		updateCodeList("");
	}

	private void setUpListeners() {
		mTextReset.setOnClickListener(this);
	}

	private void updateCodeList(String inputCode) {
		// 更新选择的图案
		mLockIndicator.setPath(inputCode);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.top_layout:
				this.finish();
				break;
			case R.id.text_reset:
				if(flag.equals("0")){
					ActivityManager.getInstance().exitActivity();
					startActivity(new Intent(GestureEditActivity.this, MainActivity.class));
					Intent mIntent = new Intent(ACTION_NAME);
					mIntent.putExtra("yaner", "shouye");
					sendBroadcast(mIntent);
					mTextReset.setText("跳过手势密码设置");
				}else {
					mIsFirstInput = true;
					updateCodeList("");
					mTextReset.setVisibility(View.GONE);
					mTextTip.setText("您好，画一个解锁手势");
				}
				break;
			default:
				break;
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		if(flag.equals("0")) {
			ActivityManager.getInstance().exitActivity();
			startActivity(new Intent(GestureEditActivity.this, MainActivity.class));
			Intent mIntent = new Intent(ACTION_NAME);
			mIntent.putExtra("yaner", "shouye");
			sendBroadcast(mIntent);
		}else {
			this.finish();
		}
	}

	private boolean isInputPassValidate(String inputPassword) {
		if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4) {
			return false;
		}
		return true;
	}

}
